﻿<?xml version="1.0" encoding="UTF-8" ?>

<!-- This is a Xsl To .NET Code Generator Template; the Xsl To .NET Code Generator Visual Studio 2010
     Add-in is required for the automatic code generation.
     http://www.codeplex.com/CodeGenerator 
-->

<!-- Converts the xsd schema file to C# code. -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                              xmlns:user="urn:my-scripts"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              xmlns:xs="http://www.w3.org/2001/XMLSchema"
                              xmlns:codegen="http://www.codeplex.com/codegenerator">
  <xsl:output method = "text" omit-xml-declaration = "no" indent = "no" encoding="Windows-1252"/>

  <!-- A set of C# functions that extend the Xsl language while generating the code. -->
  <msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
  public DateTime LastUpdatedOn()
  {      
      return DateTime.Now;
  }
  ]]>
  </msxsl:script>
  
  <!-- Represents a single empty space. -->
  <xsl:variable name="space" xml:space="preserve" > </xsl:variable>

  <!-- Start the code generation here. -->
  <xsl:template match ="*">

    // This code is automatically generated, if you modify this file manually, your changes will be lost!
    // Last Updated On: <xsl:value-of select ="user:LastUpdatedOn()" />

    #pragma warning disable 1591

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    namespace <xsl:value-of select="codegen:GetDefaultNamespace()"/>
    {
    <xsl:for-each select="/xs:schema/xs:element">
      <xsl:call-template name ="generate_class">
        <xsl:with-param name="DocumentNamespace" select ="/xs:schema/@targetNamespace"/>
      </xsl:call-template>
    </xsl:for-each>
    }

  </xsl:template>

  <xsl:template name="generate_class">
    <xsl:param name ="DocumentNamespace" />
    <xsl:choose>

      <xsl:when test ="@type">
        <xsl:call-template name ="generate_object"/>
      </xsl:when>

      <xsl:otherwise>
        <xsl:if test ="string-length($DocumentNamespace) = 0">
          public <xsl:value-of select="@name"/> <xsl:value-of select="$space"/> <xsl:value-of select="@name"/>Element;
        </xsl:if>
        public partial class <xsl:value-of select="@name"/>
        {
        <xsl:if test ="$DocumentNamespace">
          public static XNamespace DocumentNamespace = "<xsl:value-of select="$DocumentNamespace" />";

          public override string ToString()
          {
          return this.ToXmlElement().ToString();
          }
        </xsl:if>

        public XElement ToXmlElement()
        {
        return new XElement(DocumentNamespace + "<xsl:value-of select="@name"/>"
        <xsl:for-each select="xs:complexType/xs:all/xs:element">
          <xsl:choose>
            <xsl:when test ="@type">
              , new XElement(DocumentNamespace + "<xsl:value-of select="@name"/>", this.<xsl:value-of select="@name"/>)
            </xsl:when>
            <xsl:otherwise>
              , this.<xsl:value-of select="@name"/>Element != null ?
              this.<xsl:value-of select="@name"/>Element.ToXmlElement():
              new XElement(DocumentNamespace + "<xsl:value-of select="@name"/>", null)
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        );
        }

        <xsl:for-each select="xs:complexType/xs:all/xs:element">
          <xsl:call-template name ="generate_class"/>
        </xsl:for-each>
        }
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="generate_object">
    <xsl:choose>
      <xsl:when test ="@type = 'xs:string'">
        public string <xsl:value-of select="@name"/>;
      </xsl:when>

      <xsl:when test ="@type = 'xs:int' and @nillable='false'">
        public int <xsl:value-of select="@name"/>;
      </xsl:when>
      <xsl:when test ="@type = 'xs:int' and @nillable='true'">
        public int? <xsl:value-of select="@name"/>;
      </xsl:when>

      <xsl:when test ="@type = 'xs:long' and @nillable='false'">
        public long <xsl:value-of select="@name"/>;
      </xsl:when>
      <xsl:when test ="@type = 'xs:long' and @nillable='true'">
        public long? <xsl:value-of select="@name"/>;
      </xsl:when>

      <xsl:when test ="@type = 'xs:dateTime' and @nillable='false'">
        public DateTime <xsl:value-of select="@name"/>;
      </xsl:when>
      <xsl:when test ="@type = 'xs:dateTime' and @nillable='true'">
        public DateTime? <xsl:value-of select="@name"/>;
      </xsl:when>
    </xsl:choose>

  </xsl:template>

</xsl:stylesheet>